package com.unience.ec2.eclipse.model;

import java.util.ArrayList;
import java.util.List;

import com.xerox.amazonws.ec2.ImageDescription;

public class Ec2Info extends Model {
	protected List images;
	protected List securityGroups;
	protected List ec2Instances;
	protected List ec2InfoList;
	
	private static IModelVisitor adder = new Adder();
	private static IModelVisitor remover = new Remover();
	
	public Ec2Info() {
		images = new ArrayList();
		securityGroups = new ArrayList();
		ec2Instances = new ArrayList();
		ec2InfoList = new ArrayList();
	}
	
	private static class Adder implements IModelVisitor {

		/*
		 * @see ModelVisitorI#visitBoardgame(SecurityGroup)
		 */

		/*
		 * @see ModelVisitorI#visitBook(Ec2Info)
		 */

		/*
		 * @see ModelVisitorI#visitMovingBox(Ec2Info)
		 */

		

		
		public void visitEc2Image(Ec2Image ec2image,
				Object passAlongArgument) {
			((Ec2Info) passAlongArgument).addEc2Image(ec2image);
			
		}


	
		public void visitSecurityGroup(SecurityGroup secgroup,
				Object passAlongArgument) {
			((Ec2Info) passAlongArgument).addSecurityGroup(secgroup);
			
		}

		
		/*
		 * @see ModelVisitorI#visitBook(Ec2Info, Object)
		 */
		public void visitEc2Instance(Ec2Instance ec2Instance, Object argument) {
			((Ec2Info) argument).addEc2Instance(ec2Instance);
		}

		/*
		 * @see ModelVisitorI#visitMovingBox(Ec2Info, Object)
		 */
		public void visitEc2Info(Ec2Info box, Object argument) {
			((Ec2Info) argument).addEc2Info(box);
		}

	}

	private static class Remover implements IModelVisitor {
		
		public void visitEc2Image(Ec2Image imageDescrp,
				Object passAlongArgument) {
			((Ec2Info) passAlongArgument).removeEc2Image(imageDescrp);
			
		}

		public void visitSecurityGroup(SecurityGroup boardgame, Object argument) {
			((Ec2Info) argument).removeSecurityGroup(boardgame);
		}

		/*
		 * @see ModelVisitorI#visitBook(Ec2Info, Object)
		 */
		public void visitEc2Instance(Ec2Instance ec2Instance, Object argument) {
			((Ec2Info) argument).removeEc2Instance(ec2Instance);
		}

		/*
		 * @see ModelVisitorI#visitMovingBox(Ec2Info, Object)
		 */
		public void visitEc2Info(Ec2Info box, Object argument) {
			((Ec2Info) argument).removeBox(box);
			box.addListener(NullDeltaListener.getSoleInstance());
		}

	}
	
	public Ec2Info(String name) {
		this();
		this.name = name;
	}
	
	public List getImages() {
		return images;
	}
	
	protected void addEc2Info(Ec2Info ec2Info) {
		ec2InfoList.add(ec2Info);
		ec2Info.parent = this;
		fireAdd(ec2Info);
	}
	
	protected void addEc2Instance(Ec2Instance ec2Instance) {
		ec2Instances.add(ec2Instance);
		ec2Instance.parent = this;
		fireAdd(ec2Instance);
	}
	
	protected void addEc2Image(Ec2Image ec2Image) {
		images.add(ec2Image);
		ec2Image.parent = this;
		fireAdd(ec2Image);
	}
	
	protected void addSecurityGroup(SecurityGroup game) {
		securityGroups.add(game);
		game.parent = this;
		fireAdd(game);
	}		
	
	public List getEc2Instances() {
		return ec2Instances;
	}
	
	public void remove(Model toRemove) {
		toRemove.accept(remover, this);
	}
	
	protected void removeSecurityGroup(SecurityGroup secGroup) {
		securityGroups.remove(secGroup);
		secGroup.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(secGroup);
	}
	
	protected void removeEc2Instance(Ec2Instance ec2Instance) {
		ec2Instances.remove(ec2Instance);
		ec2Instance.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(ec2Instance);
	}
	
	protected void removeBox(Ec2Info box) {
		images.remove(box);
		box.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(box);	
	}

	
	
	protected void removeEc2Image(Ec2Image image) {
		images.remove(image);
		image.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(image);	
	}
	
	public void add(Model toAdd) {
		toAdd.accept(adder, this);
	}
	
	public List getSecurityGroups() {
		return securityGroups;
	}
	
	/** Answer the total number of items the
	 * receiver contains. */
	public int size() {
		return getEc2Instances().size() + getImages().size() + getSecurityGroups().size();
	}
	/*
	 * @see Model#accept(ModelVisitorI, Object)
	 */
	public void accept(IModelVisitor visitor, Object passAlongArgument) {
		visitor.visitEc2Info(this, passAlongArgument);
	}

	public List getEc2InfoList() {
		return ec2InfoList;
	}

}
